{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数值计算\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "# 绘图\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from wordcloud import WordCloud\n",
    "from imageio import imread\n",
    "\n",
    "# 浏览器控制\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "\n",
    "#网站解析\n",
    "import requests, lxml\n",
    "from bs4 import BeautifulSoup\n",
    "import jieba\n",
    "\n",
    "#自定义模块\n",
    "import package.web as web\n",
    "\n",
    "# 多线程\n",
    "from threading import Thread\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单个课程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://icourse.club/course/20920/\"\n",
    "\n",
    "comments = web.GetComment(url)\n",
    "\n",
    "# 评课长度分布直方图\n",
    "LengthDistribution = list(map(len,comments))\n",
    "sns.displot(LengthDistribution)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "web.TheWordCloud(comments)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搜索某一课程，生成不同老师的云图（多进程）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获得一个搜索结果下的所有课程链接\n",
    "links = web.SearchCourse('大物实验')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多线程获取一个搜索结果下每一个链接中的评课\n",
    "\n",
    "# 多线程获得返回值的 CommentsThread 类\n",
    "class CommentsThread(Thread):\n",
    "\n",
    "    def __init__(self, url):\n",
    "        Thread.__init__(self)\n",
    "        self.url = url\n",
    "\n",
    "    def run(self):\n",
    "        self.result = web.GetComment(self.url)\n",
    "\n",
    "    def get_result(self):\n",
    "        return self.result\n",
    "\n",
    "\n",
    "\n",
    "# 初始化线程\n",
    "comments_thd = list(map(CommentsThread,links))\n",
    "\n",
    "#多进程的开始与结束\n",
    "for threads in comments_thd:\n",
    "    threads.start()\n",
    "for threads in comments_thd:\n",
    "    threads.join()\n",
    "    \n",
    "# 综合一下结果\n",
    "all_course_comments = []\n",
    "for i in range(len(links)):\n",
    "    all_course_comments.append(comments_thd[i].get_result())\n",
    "\n",
    "len(all_course_comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多线程绘图 CloudThread 类\n",
    "class CloudThread(Thread):\n",
    "\n",
    "    def __init__(self, comments, number):\n",
    "        Thread.__init__(self)\n",
    "        self.comments = comments\n",
    "        self.number = number\n",
    "\n",
    "    def run(self):\n",
    "        self.result = web.TheWordCloud(self.comments,self.number)\n",
    "\n",
    "    def get_result(self):\n",
    "        return self.result\n",
    "\n",
    "# 初始化线程\n",
    "\n",
    "cloud_thd = []\n",
    "for i in range(len(all_course_comments)):\n",
    "    cloud_thd.append(CloudThread(all_course_comments[i],i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cloud_thd[9].start()\n",
    "cloud_thd[9].join()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
